import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as e
from selenium.webdriver.remote.webdriver import WebElement

#默认时间
TIMEOUT = 8

class Base():
    #初始化驱动
    def __init__(self,driver):
        self.driver = driver

    #封装目标网址
    def goto(self,url):
        self.driver.get(url)

    #封装显示等待点击
    def wait_click(self,loc,timeout=None) -> WebElement:
        timeout = TIMEOUT if timeout is None else timeout

        wait = WebDriverWait(self.driver,timeout=timeout)

        return wait.until(e.element_to_be_clickable(loc))

    #封装显示等待
    def wait_text(self,loc,timeout=None) -> WebElement:
        timeout = TIMEOUT if timeout is None else timeout
        wait = WebDriverWait(self.driver,timeout=timeout)

        return wait.until(e.visibility_of_element_located(loc))

    #点击
    def click(self,loc):
        e = self.wait_click(loc)
        e.click()

    #输入
    def send_keys(self,loc,text):
        e = self.wait_click(loc)
        e.send_keys(text)

    #文本
    def text(self,loc):
        return self.wait_text(loc).text

    #强制等待
    def sleep(self,timeout):
        time.sleep(10)